package cn.zebra.dev.member.module.login.service;

import cn.zebra.dev.base.module.member.entity.BaseMember;
import cn.zebra.dev.member.module.security.entity.MemberDetailsEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

/**
 * @author runnable@sina.cn
 */
@Service
public class SecurityMemberService {

    /**
     * 获取当前用户的头像
     *
     * @return 头像图片路径
     */
    public String getMemberPhoto() {
        return getBaseMember().getMemberHeadImg();
    }

    /**
     * 获取当前登录用户信息(所有)
     *
     * @return Security用户对象
     */
    public MemberDetailsEntity getMember() {
        SecurityContext context = SecurityContextHolder.getContext();
        Authentication authentication = context.getAuthentication();
        return (MemberDetailsEntity) authentication.getPrincipal();
    }

    public BaseMember getBaseMember() {
        return getMember().getMember();
    }

    /**
     * 获取当前登录用户的手机号
     *
     * @return 用户手机号码
     */
    public String getMemberPhone() {
        return getBaseMember().getPhone();
    }

    /**
     * 获取当前登录用户Id
     *
     * @return 用户id
     */
    public Long getMemberId() {
        return getBaseMember().getId();
    }

    /**
     * 获取当前登录用户名
     *
     * @return 用户名
     */
    public String getMemberName() {
        return getBaseMember().getMemberName();
    }

    /**
     * 获取当前登录用户邮箱
     *
     * @return 邮箱
     */
    public String getMemberEmail() {
        return getBaseMember().getEmail();
    }
}
